package FulbitoFacil.Servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import FulbitoFacil.excepcion.DAOExcepcion;
import FulbitoFacil.modelo.Persona;
import FulbitoFacil.negocio.GestionPersona;
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String correo = request.getParameter("txtUsuario");
		String clave = request.getParameter("txtClave");

		GestionPersona negocio = new GestionPersona();

		try {
			Persona per = negocio.validarSolicitud(correo, clave);
			// String tipoPer = per.getTipoPers();
		
			System.out.println(per.getIdPersona());
			if( per.getIdPersona() == 0 ){
				// logeo incorrecto
				request.setAttribute("ERROR", "Login incorrecto");
				RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
				rd.forward(request, response);
				
				
			} else {
		
				// correcto
				HttpSession session = request.getSession();
				session.setAttribute("USUARIO_ACTUAL", per);
				if (per.getTipoPers().equals("due"))
					response.sendRedirect("dueno.jsp");
				else if (per.getTipoPers().equals("cli"))
					response.sendRedirect("cliente.jsp");
				else if (per.getTipoPers().equals("adm"))
					response.sendRedirect("administrator.jsp");
			}
			
//			System.out.println("uno");
//			System.out.println(per.getTipoPers());
//			System.out.println("dos");
//			if (per.getTipoPers().equals(null)) {
//		
//			} else {
//				RequestDispatcher rd = request C
//						.getRequestDispatcher("formPersona.jsp");
//				rd.forward(request, response);
//			}

//			return;

		} catch (DAOExcepcion e) {
	
			
			request.setAttribute("ERROR", e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
			rd.forward(request, response);
			
		}

		// TODO Auto-generated method stub

		// String x = request.getParameter("txtComentario");

		// String cadena = "Bienvenido: " + x;

		// request.setAttribute("MSG", cadena);

		// RequestDispatcher rd = request.getRequestDispatcher("principal.jsp");
		// rd.forward(request, response);

	}

}
